package com.sistema.thingsControl;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class HibernateUtil {

	private static final SessionFactory SESSION_FACTORY = HibernateUtil.buildSessionFactory();

	private static SessionFactory buildSessionFactory() {

		try {
			Configuration configuration = new Configuration();
			configuration.configure("hibernate.cfg.xml");
			return configuration.buildSessionFactory();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ExceptionInInitializerError();
		}

	}

	public static SessionFactory getSession(){
		return HibernateUtil.SESSION_FACTORY;
	}

	public static void main(String[] args) {

		System.out.println("open");

		Session sessao = null;

		try {
			sessao = HibernateUtil.getSession().openSession();
			System.out.println("open");
		} finally {
			sessao.close();
			System.out.println("close");
		}

	}

}
